#include "SecurityCannon.h"
#include "Physics.h"
#include "Player.h"
#include "GamePlayState.h"
#include "Camera.h"

#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "Events\CSGD_EventSystem.h"

#define RANGE 400.0f

SecurityCannon::SecurityCannon(void)
{
	objectID = "Cannon";

	TurretBaseID = -1;

	this->Rotation = 0.0f;
	this->LaserTimer = 0.0f;
	FireTheLaser = false;
}

SecurityCannon::~SecurityCannon(void)
{
}

void SecurityCannon::Update(float deltaTime)
{
	//Can we Fire the lasers?
	if( FireTheLaser == true )
	{
		LaserTimer -= deltaTime;
		if( LaserTimer <= 0.0f )
		{
			//Reset the timer
			LaserTimer = 0.0f;

			//KILL IT WITH FIRE
			CSGD_EventSystem::GetInstance()->SendEvent("KillPlayer", nullptr, nullptr, (void*)this);

			//Reset the rotation
			Rotation = 0.0f;

			//Reset
			FireTheLaser = false; 
		}
	}




	//Get a ref to the player
	Player* PlayerAcc = GamePlayState::GetInstance()->GetPlayer();

	//Get the sec camera width and height
	int SecCamWidth = hitBox.right - hitBox.left;
	int SecCamHeight = hitBox.bottom - hitBox.top;

	//Player Postion 
	tVector2D PlayerPosition = {PlayerAcc->GetPosX(), PlayerAcc->GetPosY()};

	//This Current Position
	tVector2D CameraCenterPos = { hitBox.left + (SecCamWidth * 0.5f), hitBox.top + (SecCamHeight * 0.5f)};

	//Get the distance between this and the player
	tVector2D DistanceVec = PlayerPosition - CameraCenterPos;

	//Get the length of that vector
	float Length = Vector2DLength(DistanceVec);

	//Normalize dist
	//DistanceVec = Vector2DNormalize(DistanceVec);



	//Check if the player is in range
	if( Length < RANGE )
	{
		//Rotate toward the player
		tVector2D Orientation = {0, 1};

		//Set the orientation to the current rotation
		Orientation = Vector2DRotate( Orientation, Rotation );

		//Calculate the angle between the cannon and the player
		float Angle = AngleBetweenVectors( Orientation, DistanceVec);

		//If the angle between is 0, cannon and player are lined up. FIRE THE LASERRRRRR
		if(Angle < 0.01f && Angle > -0.01f && FireTheLaser == false)
		{
			FireTheLaser = true;
			LaserTimer = 0.5f;
		}


		//Limit the rotation
		float Max = D3DX_PI * 0.5f * deltaTime;
		if( Angle > Max )
			Angle = Max;

		if( Steering( Orientation, DistanceVec ) < 0.0f )
			Angle = -Angle;

		//Set Rotation
		this->Rotation += Angle;
	}
	else
	{
		//Player is not in range. Rotate Back
		float MoveBackSpeed = D3DX_PI / 4 * deltaTime;

		if( Rotation > 0.0f )
			Rotation -= MoveBackSpeed;
		else
			Rotation = 0.0f;

	}



}
void SecurityCannon::Render(void) const
{
	//Render the Cannon
	if(imageID != -1)
	{
		RECT SourceRect = {0,0,32,64};
		//CSGD_TextureManager::GetInstance()->Draw(imageID,(int)(positionX - Camera::GetInstance()->OffsetX()),(int)(positionY - Camera::GetInstance()->OffsetY() ),1.0f,1.0f,&SourceRect,16,2,Rotation);
		CSGD_TextureManager::GetInstance()->Draw(imageID,(int)(positionX - Camera::GetInstance()->OffsetX()),(int)(positionY - Camera::GetInstance()->OffsetY() ),1.0f,1.0f,&SourceRect,16,2,Rotation);

	}

	//Draw the circle for the base of the turret
	if( TurretBaseID != -1 )
	{
		RECT SourceRect = {0,0,64,64};
		CSGD_TextureManager::GetInstance()->Draw(TurretBaseID,(int)(positionX - Camera::GetInstance()->OffsetX()  - 3),(int)(positionY - Camera::GetInstance()->OffsetY() - 18),0.6f,0.6f,&SourceRect,32.0f,32.0f,0.0f);
	}

	//Can we Fire the lasers?
	if( FireTheLaser == true )
	{
		//Get a ref to the player
		Player* PlayerAcc = GamePlayState::GetInstance()->GetPlayer();

		//Get the sec camera width and height
		int SecCamWidth = hitBox.right - hitBox.left;
		int SecCamHeight = hitBox.bottom - hitBox.top;

		//Player Postion 
		tVector2D PlayerPosition = {PlayerAcc->GetPosX(), PlayerAcc->GetPosY()};

		//This Current Position
		tVector2D CameraCenterPos = { hitBox.left + (SecCamWidth * 0.5f), hitBox.top + (SecCamHeight * 0.5f)};

		//Get the player center
		tVector2D PlayerCenter = { PlayerAcc->GetHitBox().left + (PlayerAcc->GetHitBox().right - PlayerAcc->GetHitBox().left) * 0.5f, PlayerAcc->GetHitBox().top + (PlayerAcc->GetHitBox().bottom - PlayerAcc->GetHitBox().top) * 0.5f };

		//Draw the laser
		CSGD_Direct3D::GetInstance()->DrawLine(
			(int)(CameraCenterPos.fX - Camera::GetInstance()->OffsetX()), 
			(int)(CameraCenterPos.fY - Camera::GetInstance()->OffsetY()), 
			(int)(PlayerCenter.fX - Camera::GetInstance()->OffsetX()),
			(int)(PlayerCenter.fY - Camera::GetInstance()->OffsetY()),
			D3DCOLOR_ARGB(255, 255, 0, 0), 3);
	}
}
void SecurityCannon::HandleCollision(GameObject& other, CollisionInfo& info)
{
	
}

void SecurityCannon::SetTurretBaseID( int index )
{
	this->TurretBaseID = index;
}